//
//  QXRoundRectView.m
//  Pods
//
//  Created by Qianxia on 2016/12/23.
//
//

#import "QXRoundRectView.h"

@implementation QXRoundRectView

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    //设置边框颜色
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    //设置边框宽度
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    //设置圆角
    self.layer.cornerRadius = cornerRadius;
}

- (void)setMasksToBounds:(BOOL)masksToBounds {
    _masksToBounds = masksToBounds;
    
    self.layer.masksToBounds = masksToBounds;
}

@end


@implementation QXCustomRoundRectView

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    
    [self setNeedsDisplay];
}

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    
    [self setNeedsDisplay];
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    
    [self setNeedsDisplay];
}

- (void)setFilled:(BOOL)filled {
    _filled = filled;
    
    [self setNeedsDisplay];
}

- (void)setFillColor:(UIColor *)fillColor {
    _fillColor = fillColor;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    if (!self.roundingCorner) {
        self.roundingCorner = UIRectCornerAllCorners;
    }
    
    // Drawing code
    CGRect boundsRect = CGRectMake(self.borderWidth / 2, self.borderWidth / 2, rect.size.width - self.borderWidth, rect.size.height - self.borderWidth);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:boundsRect byRoundingCorners:self.roundingCorner cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
    [path closePath];
    [path addClip];
    
    // 判断是否填充颜色
    if (self.filled && self.fillColor) {
        [self.fillColor setFill];
        [path fill];
    }
    
    // 设置边框颜色
    if (self.borderColor) {
        [self.borderColor setStroke];
        path.lineWidth = self.borderWidth;
        [path stroke];
    }
}

@end

#pragma mark - Button

@implementation QXRoundRectButton

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    //设置边框颜色
    self.layer.borderColor = borderColor.CGColor;
}


- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    //设置边框宽度
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    //设置圆角
    self.layer.cornerRadius = cornerRadius;
}

/**
 *  @brief  设置高亮颜色
 *
 */
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    
    // 设置高亮颜色
    if (highlighted && self.highlightedBackgroundColor) {
        
        self.backgroundColor = self.highlightedBackgroundColor;
    } else if (self.normalBackgroundColor) {
        
        self.backgroundColor = self.normalBackgroundColor;
    }
}

@end

#pragma mark - UIControl

@implementation QXRoundRectControl


- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    //设置边框颜色
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    //设置边框宽度
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    //设置圆角
    self.layer.cornerRadius = cornerRadius;
}

@end

#pragma mark - UIImageView

@implementation QXRoundRectImageView


- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    //设置边框颜色
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    //设置边框宽度
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    //设置圆角
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}


@end



@implementation QXRoundRectTextView


- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    
    // 设置边框颜色
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    
    // 设置边框宽度
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    
    // 设置圆角
    self.layer.cornerRadius = cornerRadius;
}


@end


@implementation QXSingleBox

- (void)setColor:(UIColor *)color {
    _color = color;
    
}

- (void)setIsSeleted:(BOOL)isSeleted {
    _isSeleted = isSeleted;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    //获取宽度
    CGFloat width = MIN(CGRectGetWidth(rect), CGRectGetHeight(rect));
    
    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(rect.origin.x + self.lineWidth, rect.origin.y + self.lineWidth, width - 2 * self.lineWidth, width - self.lineWidth * 2)];
    [self.color setStroke];
    ovalPath.lineWidth = self.lineWidth;
    [ovalPath stroke];
    
    if (self.isSeleted) {
        CGRect ovalRect = CGRectMake(CGRectGetMinX(rect) + self.spacing + self.lineWidth, CGRectGetMinY(rect) + self.spacing + self.lineWidth, width - (self.spacing + self.lineWidth)*2, width - (self.spacing + self.lineWidth) * 2);
        //// Oval 2 Drawing
        UIBezierPath* oval2Path = [UIBezierPath bezierPathWithOvalInRect: ovalRect];
        [self.color setFill];
        [oval2Path fill];
    }
}

@end


@implementation QXSingleColorBox

- (void)setColor:(UIColor *)color {
    _color = color;
    
}

- (void)setIsSeleted:(BOOL)isSeleted {
    _isSeleted = isSeleted;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    //获取宽度
    CGFloat width = MIN(CGRectGetWidth(rect), CGRectGetHeight(rect));
    
    UIColor *color = [UIColor darkGrayColor];
    
    if (self.isSeleted) {
        color = self.color ? : color;
    }
    else {
        color = self.defaultColor ? : color;
    }
    
    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(rect.origin.x + self.lineWidth, rect.origin.y + self.lineWidth, width - 2 * self.lineWidth, width - self.lineWidth * 2)];
    [color setStroke];
    ovalPath.lineWidth = self.lineWidth;
    [ovalPath stroke];
    
    CGRect ovalRect = CGRectMake(CGRectGetMinX(rect) + self.spacing + self.lineWidth, CGRectGetMinY(rect) + self.spacing + self.lineWidth, width - (self.spacing + self.lineWidth)*2, width - (self.spacing + self.lineWidth) * 2);
    //// Oval 2 Drawing
    UIBezierPath* oval2Path = [UIBezierPath bezierPathWithOvalInRect: ovalRect];
    [color setFill];
    [oval2Path fill];
    
}

@end


@implementation QXRoundRectLabel

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    //设置边框颜色
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    //设置边框宽度
    self.layer.borderWidth = borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    //设置圆角
    self.layer.cornerRadius = cornerRadius;
}

@end



@implementation QXStretchableImageView

- (void)setLeftCapWidth:(NSInteger)leftCapWidth {
    _leftCapWidth = leftCapWidth;
    
    [self setImageStretchable];
}

- (void)setTopCapHeight:(NSInteger)topCapHeight {
    _topCapHeight = topCapHeight;
    
    [self setImageStretchable];
}

- (void)setImageStretchable {
    self.image = [self.image stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight];
}

@end

@implementation QXDashedLine

- (void)setThickness:(CGFloat)thickness {
    _thickness = thickness;
    
    [self setNeedsDisplay];
}

- (void)setIntensive:(CGFloat)intensive {
    _intensive = intensive;
    
    [self setNeedsDisplay];
}

- (void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    
    [self setNeedsDisplay];
}

- (void)setIsVertical:(BOOL)isVertical {
    _isVertical = isVertical;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, self.thickness);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    
    // 12是设置密集程度
    CGFloat lengths[] = {self.intensive, 0};
    CGContextSetLineDash(context, 0.0, lengths, 3);
    
    if (self.isVertical) {
        
        CGContextMoveToPoint(context, self.thickness * 0.5, 0);
        CGContextAddLineToPoint(context, self.thickness * 0.5, CGRectGetWidth(self.bounds));
    } else {
        
        CGContextMoveToPoint(context, 0, self.thickness * 0.5);
        CGContextAddLineToPoint(context, CGRectGetWidth(self.bounds), self.thickness * 0.5);
    }
    
    CGContextStrokePath(context);
}

@end
